<template>
  <div class="parsing-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="header__item">
          <span class="label">选择模板</span>
          <el-select v-model="modelId" size="mini" placeholder="请选择" @change="onModelIdChange">
            <el-option v-for="item in optionsOfModel" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
        <div v-show="modelId !== 1" class="header__item">
          <span class="label">数据时间</span>
          <el-date-picker
            v-model="date"
            size="mini"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          />
        </div>

        <upload
          v-show="modelId !== 1"
          style="margin-left: 8px;"
          :action="uploadPath"
          :query="uploadQuery"
          select-label="加载报表"
          upload-lable="解析"
          @on-succeed="onUploadSucceed"
          @on-fail="onUploadFail"
        />

        <el-button class="header__right" type="text">!</el-button>
        <el-divider class="header__right header-divider" direction="vertical" />
        <el-button class="header__right" type="text" @click="handleApprove">提交结果</el-button>
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column label="数据名称" prop="standardCode" sortable :formatter="formatterOfName" />
        <!-- <el-table-column label="解析结果" prop="value" sortable :formatter="formatterOfvalue" /> -->
        <el-table-column label="解析结果" prop="value">
          <template slot-scope="{ row }">
            <span v-if="modelId !== 1">{{ row.value }}</span>
            <el-input-number v-else v-model="row.value" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="valueStatus" sortable>
          <template slot-scope="{ row }">
            <span :style="row.valueStatus | statusBackgroundColor">
              {{ row.valueStatus | formatterOfStatus }}
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="数据时间" prop="time" sortable :formatter="formatterOfTime" /> -->
        <el-table-column label="数据时间" prop="time">
          <template slot-scope="{ row }">
            <span v-if="modelId !== 1">{{ row.time | dateformat }}</span>
            <el-date-picker
              v-else
              v-model="row.time"
              type="datetime"
              size="mini"
              placeholder="选择日期时间"
            />
          </template>
        </el-table-column>
      </el-table>

      <!-- <div class="foot-wrapper">
        <el-pagination
          :total="20"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100, 200]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div> -->
    </el-card>

    <div v-if="!!errorMgs" class="error-info">
      <span>{{ errorMgs }}</span>
      <el-button type="text" icon="el-icon-close" size="mini" class="error-btn" @click="closeErrMsg" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import moment from 'moment'
import { Upload } from './components'
import * as api from '@/api/model'

const colorFormStatus = (status) => {
  return ['yellow', 'cornflowerblue', 'darkgray'][status] || '#fff'
}

const statusBackgroundColor = (status) => {
  return {
    backgroundColor: colorFormStatus(status)
  }
}

export default {
  name: 'ParsingPage',
  components: {
    Upload
  },
  filters: {
    statusBackgroundColor,
    formatterOfStatus: (status) => ['正常', '可能不正常', '异常'][status],
    dateformat: (date) => moment(date).format('YYYY-MM-DD HH:mm:ss')
  },
  data() {
    return {
      currentPage: 1,
      modelId: null,
      date: '',
      list: [],
      errorMgs: ''
    }
  },
  computed: {
    ...mapState({
      models: state => state.model.list,
      standardCodes: state => state.dataset.standardCode
    }),
    optionsOfModel() {
      return this.models.map(m => ({
        value: m.id,
        label: m.name
      }))
    },
    uploadQuery() {
      return {
        modelId: this.modelId || undefined,
        date: this.date || undefined
      }
    },
    uploadPath() {
      return `${process.env.VUE_APP_BASE_API}/excel/parse/data`
    }
  },
  created() {
    this.$store.dispatch('dataset/load')
    this.$store.dispatch('model/fetchList')
  },
  methods: {
    formatterOfTime(row, column, cellValue, index) {
      return moment(cellValue).format('YYYY-MM-DD HH:mm:ss')
    },
    formatterOfStatus(row, column, cellValue, index) {
      const ss = ['正常', '可能不正常', '异常']
      return ss[cellValue]
    },
    formatterOfvalue(row, column, cellValue, index) {
      return cellValue || '--'
    },
    formatterOfName(row, column, cellValue, index) {
      const item = this.standardCodes.find(it => it.code === cellValue)
      return item && item.name || '--'
    },
    handleSizeChange() {
    },
    handleCurrentChange() {
    },
    onUploadSucceed(data) {
      this.list = data
    },
    onUploadFail(error) {
      this.errorMgs = error.message
    },
    closeErrMsg() {
      this.errorMgs = ''
    },
    async handleApprove() {
      try {
        const modelId = this.modelId
        const model = this.optionsOfModel.find(m => m.value === modelId)
        const fileName = model && model.label
        await this.$store.dispatch('model/approve', { modelId, fileName, data: this.list })
        this.$message.success('提交成功')
      } catch (error) {
        this.$message.error(error)
      }
    },
    async onModelIdChange(value) {
      if (value === 1) {
        const res = await api.getDetail(value)
        this.list = res.tags.map(l => ({ ...l, valueStatus: 0 }))
      } else {
        this.list = []
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.parsing {
  &-container {
    margin: 16px;
    .box-card {
      .header__item{
        display: inline-flex;
        width: 220px;
        margin-left: 16px;
      }
      .label {
        width: 90px;
        font-size: 14px;
        line-height: 28px;
      }
      .header__left {
        float: left;
      }
      .header__right {
        float: right;
        padding: 3px 0;
      }
      .header-divider {
        height: 1.2em;
      }
      .row-text {
        button {
          display: none;
        }
        &:hover {
          button {
            display: inline-block;
          }
        }
      }
      .card-footer {
        margin-top: 32px;
      }
      .foot-wrapper {
        .el-pagination {
            float: right;
            margin-top: 10px;
        }
      }
    }
    .error-info {
      background-color: #fef0f0;
      border-color: #fde2e2;
      color: #F56C6C;
      padding: 12px;
      margin-top: 16px;
      font-size: 14px;
      .error-btn {
        float: right;
        color: #F56C6C;
        display: none;
      }
      &:hover {
        .error-btn {
          display: block;
        }
      }
    }
  }
}
</style>
